using System.IO;
using System.Runtime.InteropServices;
using System.Text;

public class AccessIniFile
{
	[DllImport("kernel32")]
	private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

	[DllImport("kernel32")]
	private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

	[DllImport("Kernel32.dll")]
	public static extern int GetPrivateProfileInt(string strAppName, string strKeyName, int nDefault, string strFileName);

	public static string ReadValue(string iniFilePath, string section, string key)
	{
		if (File.Exists(iniFilePath))
		{
			StringBuilder stringBuilder = new StringBuilder(255);
			if (GetPrivateProfileString(section, key, "", stringBuilder, 255, iniFilePath) != 0)
			{
				return stringBuilder.ToString();
			}
		}
		return string.Empty;
	}

	public static int ReadIntValue(string iniFilePath, string section, string key, int defaultValue = 0)
	{
		return GetPrivateProfileInt(section, key, defaultValue, iniFilePath);
	}

	public static double ReadDoubleValue(string iniFilePath, string section, string key)
	{
		string s = ReadValue(iniFilePath, section, key);
		double result = 0.0;
		double.TryParse(s, out result);
		return result;
	}

	public static bool WriteValue(string iniFilePath, string section, string key, string value)
	{
		if (WritePrivateProfileString(section, key, value, iniFilePath) == 0)
		{
			return false;
		}
		return true;
	}
}
